/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.bezrabotnih.cabinet.client.ui;

import java.util.ArrayList;
import java.util.List;

import net.bezrabotnih.client.managed.request.JobTypeProxy;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.IntegerBox;

/**
 * Sample implementation of {@link ProfileEditView}.
 */
public class ProfileEditViewImpl extends Composite implements ProfileEditView {

	interface Binder extends UiBinder<Widget, ProfileEditViewImpl> {
	}
	
	private static final Binder binder = GWT.create(Binder.class);

	private static ProfileEditView instance;

	private Presenter listener;
	@UiField
	Button saveButton;
	@UiField TextBox phoneNumber;
	@UiField TextBox name;
	@UiField RadioButton maleRadioButton;
	@UiField RadioButton performerRadioButton;
	@UiField RadioButton providerRadioButton;
	@UiField VerticalPanel jobTypesPanel;
	@UiField IntegerBox age;
	@UiField RadioButton femaleRadioButton;
	

	boolean allJobTypesListInited;

	

	public ProfileEditViewImpl() {
		initWidget(binder.createAndBindUi(this));
		performerRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				jobTypesPanel.setVisible(event.getValue());
//				System.out.println("performerRadioButton:"+event.getValue());
				
				
			}
		});
		providerRadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> event) {
				jobTypesPanel.setVisible(!event.getValue());
//				System.out.println("providerRadioButton:"+event.getValue());
				
			}
		});
	}
	
	public static ProfileEditView instance(){
		if (instance==null) instance=new ProfileEditViewImpl();
		return instance;
	}
	
	public boolean isAllJobTypesListInited() {
		return allJobTypesListInited;
	}

	@Override
	public void setName(String name) {
	    this.name.setText(name);
	}

	@Override
	public void setPresenter(Presenter listener) {
		this.listener = listener;
	}

	@UiHandler("saveButton")
	void onSaveButtonClick(ClickEvent event) {
		listener.saveClicked();
	}

	@Override
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber.setText(phoneNumber);
		
	}

	@Override
	public void setAge(int age) {
		this.age.setValue(age);

	}

	@Override
	public void setSex(boolean isMale) {
		this.maleRadioButton.setValue(isMale);
		this.femaleRadioButton.setValue(!isMale);
		
	}

	@Override
	public void setAccountType(boolean isPerformer) {
		this.performerRadioButton.setValue(isPerformer);
		this.providerRadioButton.setValue(!isPerformer);
		this.jobTypesPanel.setVisible(isPerformer);
		
	}

	@Override
	public void setJobTypes(List<String> selectedJobTypeIds) {
		for (int i=0;i<jobTypesPanel.getWidgetCount();i++){
			Widget widget=jobTypesPanel.getWidget(i);
			if (widget instanceof CheckBox){
				CheckBox checkBox=(CheckBox)widget;
				checkBox.setValue(selectedJobTypeIds.contains(checkBox.getName()));
			}}
		
	}

	@Override
	public String getName() {
		return this.name.getText();
	}

	@Override
	public String getPhoneNumber() {
		return this.phoneNumber.getText();
	}

	@Override
	public Integer getAge() {
		return this.age.getValue();
	}

	@Override
	public boolean getSex() {
		return this.maleRadioButton.getValue();
	}

	@Override
	public Boolean getAccountType() {
		return this.performerRadioButton.getValue();
	}

	@Override
	public List<String> getSelectedJobTypeIds() {
		List<String> result=new ArrayList<String>();
		for (int i=0;i<jobTypesPanel.getWidgetCount();i++){
			Widget widget=jobTypesPanel.getWidget(i);
			if (widget instanceof CheckBox){
				CheckBox checkBox=(CheckBox)widget;
				if (checkBox.getValue()) result.add(checkBox.getName());
			}
		}
		
		return result;
	}
	
	public void initAllJobTypesList(List<JobTypeProxy> allJobTypeProxies){
		for (JobTypeProxy jobTypeProxy:allJobTypeProxies){
			CheckBox checkBox=new CheckBox(jobTypeProxy.getName());
			checkBox.setName(jobTypeProxy.getId());
			jobTypesPanel.add(checkBox);
		}
		
		allJobTypesListInited=true;
	}
	
}
